package com.example.sunmiv1.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.sunmiv1.R;
import com.example.sunmiv1.activity.StringCodeOperationActivity;
import com.example.sunmiv1.entity.GoodsSaleListInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GoodsSaleAdapter extends BaseAdapter {

    private Context context;
    private List<GoodsSaleListInfo.SellListBean> listBeans;
    private ViewHolder viewHolder;

    public GoodsSaleAdapter(Context context, List<GoodsSaleListInfo.SellListBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
    }

    @Override
    public int getCount() {
        return listBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return listBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.goods_sale_adapter, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        GoodsSaleListInfo.SellListBean sellListBean = listBeans.get(position);
        viewHolder.goodNameSaleAdapter.setText(sellListBean.getGoods_name());
        viewHolder.freightNumberSaleAdapter.setText(sellListBean.getGoods_number());
        viewHolder.purchasePriceSaleAdapter.setText(sellListBean.getPurchase_price());
        viewHolder.priceSaleAdapter.setText(sellListBean.getPrice() + "");
        viewHolder.saleNumSaleAdapter.setText(sellListBean.getNum() + "");
        viewHolder.orderTypeSaleAdapter.setText(sellListBean.getSell_mold());
        viewHolder.totalSaleAdapter.setText(sellListBean.getSubtotal() + "");
        viewHolder.timeSaleAdapter.setText(sellListBean.getAdd_time());
        viewHolder.saleCompanySaleAdapter.setText(sellListBean.getCompany());
        if ("1".equals(listBeans.get(position).getSerial_number_state())) {
            viewHolder.saleNumSaleAdapter.setTextColor(ContextCompat.getColor(context, R.color.blue_3293FA));
        } else {
            viewHolder.saleNumSaleAdapter.setTextColor(ContextCompat.getColor(context, R.color.text_666));
        }
        viewHolder.saleNumSaleAdapter.setOnClickListener(v -> {
            if ("1".equals(listBeans.get(position).getSerial_number_state())) {
                Intent intent = new Intent(context, StringCodeOperationActivity.class);
                intent.putExtra("operation", 26);
                intent.putExtra("id", listBeans.get(position).getId());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.goodName_saleAdapter)
        TextView goodNameSaleAdapter;
        @BindView(R.id.freightNumber_saleAdapter)
        TextView freightNumberSaleAdapter;
        @BindView(R.id.purchasePrice_saleAdapter)
        TextView purchasePriceSaleAdapter;
        @BindView(R.id.price_saleAdapter)
        TextView priceSaleAdapter;
        @BindView(R.id.saleNum_saleAdapter)
        TextView saleNumSaleAdapter;
        @BindView(R.id.saleCompany_saleAdapter)
        TextView saleCompanySaleAdapter;
        @BindView(R.id.orderType_saleAdapter)
        TextView orderTypeSaleAdapter;
        @BindView(R.id.total_saleAdapter)
        TextView totalSaleAdapter;
        @BindView(R.id.time_saleAdapter)
        TextView timeSaleAdapter;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
